<?php
namespace app\admin\controller;

use app\admin\model\Admin;
use app\common\controller\Backend;
use think\helper\Time;
use think\Session;
use app\admin\model\QuesTeacher;
use app\admin\model\QuesStudent;
/**
 *
 *
 * @icon fa fa-circle-o
 */
class OptionCount extends Backend
{

    /**
     * QuesMerchant模型对象
     * @var \app\admin\model\QuesMerchant
     */
    protected $model = null;
    protected $where = [];
    protected $groupby_name = '';
    protected $href_url = '';
    private $ques = ['a_1','a_2','a_3','a_4','a_11','a_12','inoculation'];
    public function _initialize()
    {
        parent::_initialize();
        $gtypeid =  $this->auth->gtypeid;
        $this -> assign('gtypeid', $gtypeid );
        $gid = $this->auth->gid;



        $timestampArr = Time::today();
        $this->where['create_time'] = ['between', "$timestampArr[0], $timestampArr[1]"];
        if($gtypeid == 1)
        {
            $this->where['college_id'] =  $gid;
        }
        if($gtypeid == 3)
        {
            $this->where['classes_id'] =  $gid;
            // $this->remodel = new Classes();
        }

        /**
         * 控制器规划：
         * 1非研究生-选项统计：CountStudent
         * 1在编人员-选项统计：CountPermanent
         * 2研究生-选项统：CountGraduate
         * 2编外人员-选项统计：CountOutside
         * 3外来务工-选项统：CountMigrant
         */
        $controller = $this -> request -> controller();
        $this -> jwc($controller);
        switch ($controller) {
            case 'CountStudent':
                $this->model = new QuesStudent();
                $this ->where['type']=1;
                $this->groupby_name ='student_id';
                $this->href_url = 'ques_student';
                array_push($this->ques,"a_8");
                break;
            case 'CountGraduate':
                $this->model = new QuesStudent();
                $this ->where['type']=2;
                $this->groupby_name ='student_id';
                $this->href_url = 'ques_graduate';
                break;
            case 'CountPermanent':
                $this->model = new QuesTeacher();
                $this ->where['type']=1;
                $this->groupby_name ='teacher_id';
                $this->href_url = 'ques_permanent';
                array_push($this->ques,"a_7");
                break;
            case 'CountOutside':
                $this->model = new QuesTeacher();
                $this ->where['type']=2;
                $this->groupby_name ='teacher_id';
                $this->href_url = 'ques_outside';
                array_push($this->ques,"a_7");
                break;
            case 'CountMigrant':
                $this->model = new QuesTeacher();
                $this ->where['type']=3;
                $this->groupby_name ='teacher_id';
                $this->href_url = 'ques_migrant';
                array_push($this->ques,"a_7");
                break;
            default:
                # code...
                break;
        }
        array_push($this->ques,"a_9");

    }


    public function index()
    {
        $total = $this->model->where($this->where)->group($this->groupby_name)->count();
        foreach ($this->ques as $key => $value) {
            $data[$value] = config("ques.{$value}");

            $rets = $this->model->field($value.',count(1) cnt')->where($this->where)->group($value)->select();
            // echo $this->model->getLastSql();
            foreach($rets as $ret){
//                if($value == 'a_10_1' || $value == 'a_10_2')
//                {
//                    if(!$ret[$value])
//                        $ret[$value] == 100;
//                    if($ret[$value] !=100)
//                        $data[$value]["answ"][$ret[$value]]['num']=$ret['cnt'];
//                }
//                else
//                {
                    if($ret[$value] == '')
                        $ret[$value] == 0;
                    $data[$value]["answ"][$ret[$value]]['num']=$ret['cnt'];
//                }


            }
        }
//         echo "<pre>";
//         print_r($data);
        // $total = $this->model->where($this->where)->group($this->groupby_name)->count();
        // $ret = $this->model->where($this->where)->group($this->groupby_name)->select();
        // $data = [];
        // foreach($ret as $key => $value){
        //     $value = $value->toArray();
        //     foreach ($value as $k => $v) {
        //         if(in_array($k, $this->ques))
        //         {
        //             if(!isset($data[$k]))
        //             {
        //                 $data[$k] = config("ques.{$k}");
        //             }
        //             if(isset($data[$k]["answ"][$v]))
        //                 $data[$k]["answ"][$v]["num"] ++;
        //         }
        //     }
        // }

        $this->assign('total', $total);
        $this->assign('list', $data);


        $province_list = $this->model->field('province,count(*) as total')
                ->where($this->where)
                ->group('province_code')
                ->order('total','desc')
                ->select();
        $province_list = collection($province_list)->toArray();
        $province_count = $this->model->field('province,count(*) as total')
                ->where($this->where)
                ->group('province_code')
                ->count();
        $this->assign('province_list', $province_list);
        $this->assign('province_count', $province_count);
        $this->assign('href_url',$this->href_url);
         return $this->fetch('/option_count/index');
    }


    protected function jwc($controller){
        $arr = [
            'CountPermanent','CountOutside','CountMigrant'
        ];

        if(in_array($controller,$arr) && $this -> auth -> id == 1823){
            $this -> assign('gtypeid', 1 );
            $this->where['college_id'] =  38;
        }
    }

}